package bootstrap

/*
Copyright 2021-2025 The k8gb Contributors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Generated by GoLic, for more details see: https://github.com/AbsaOSS/golic
*/

import (
	"context"
	"testing"

	"github.com/k8gb-io/k8gb/controllers/providers/assistant"

	"github.com/k8gb-io/k8gb/controllers/resolver"
	"github.com/stretchr/testify/assert"
	corev1 "k8s.io/api/core/v1"
	netv1 "k8s.io/api/networking/v1"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/client-go/kubernetes/scheme"
	"sigs.k8s.io/controller-runtime/pkg/client"
	"sigs.k8s.io/controller-runtime/pkg/client/fake"
)

func TestBootstrap(t *testing.T) {

	objectMeta := metav1.ObjectMeta{
		Name:      "k8gb-coredns",
		Namespace: "k8gb",
	}
	ctx := context.TODO()
	tests := []struct {
		name          string
		config        *resolver.Config
		expectedIPs   []string
		setupClient   func(_ []string) client.Client
		expectedError bool
		hasIngress    bool
	}{
		{
			name:          "CoreDNSService missing",
			expectedError: true,
			hasIngress:    false,
			config: &resolver.Config{
				K8gbNamespace: "k8gb",
			},
			setupClient: func(_ []string) client.Client {
				serviceList := &corev1.ServiceList{
					ListMeta: metav1.ListMeta{},
					Items:    []corev1.Service{},
				}
				cl := fake.NewClientBuilder().WithScheme(scheme.Scheme).WithLists(serviceList).Build()
				_ = cl.List(ctx, serviceList, &client.ListOptions{})
				return cl
			},
		},

		{
			name:          "CoreDNSService IS LoadBalancer",
			expectedError: false,
			expectedIPs:   []string{"10.0.0.1", "10.0.0.2"},
			hasIngress:    false,
			config: &resolver.Config{
				K8gbNamespace:      "k8gb",
				CoreDNSServiceType: corev1.ServiceTypeLoadBalancer,
			},
			setupClient: func(expectedIPs []string) client.Client {
				coreDNSService := &corev1.Service{
					ObjectMeta: metav1.ObjectMeta{
						Name:      objectMeta.Name,
						Namespace: objectMeta.Namespace,
						Labels: map[string]string{
							"app.kubernetes.io/name": "coredns",
						},
					},
					Spec: corev1.ServiceSpec{
						Type: corev1.ServiceTypeLoadBalancer,
					},
					Status: corev1.ServiceStatus{
						LoadBalancer: corev1.LoadBalancerStatus{
							Ingress: []corev1.LoadBalancerIngress{
								{
									IP: expectedIPs[0],
								},
								{
									IP: expectedIPs[1],
								},
							},
						},
					},
				}
				cl := fake.NewClientBuilder().WithScheme(scheme.Scheme).WithObjects(coreDNSService).Build()
				return cl
			},
		},

		{
			name:          "CoreDNSService IS NOT LoadBalancer",
			expectedError: false,
			expectedIPs:   []string{"10.0.0.1", "10.0.0.2"},
			hasIngress:    true,
			config: &resolver.Config{
				K8gbNamespace:      "k8gb",
				CoreDNSServiceType: corev1.ServiceTypeClusterIP,
			},
			setupClient: func(expectedIPs []string) client.Client {
				coreDNSService := &corev1.Service{
					ObjectMeta: metav1.ObjectMeta{
						Name:      objectMeta.Name,
						Namespace: objectMeta.Namespace,
						Labels: map[string]string{
							assistant.CoreDNSServiceLabelName: assistant.CoreDNSServiceLabelValue,
						},
					},
					Spec: corev1.ServiceSpec{
						Type: corev1.ServiceTypeClusterIP,
					},
					Status: corev1.ServiceStatus{
						LoadBalancer: corev1.LoadBalancerStatus{
							Ingress: []corev1.LoadBalancerIngress{
								{
									IP: expectedIPs[0],
								},
								{
									IP: expectedIPs[1],
								},
							},
						},
					},
				}
				ing := &netv1.Ingress{
					ObjectMeta: metav1.ObjectMeta{
						Name:      "coredns-ingress",
						Namespace: "k8gb",
						Labels: map[string]string{
							assistant.IngressLabelName: assistant.IngressLabelValue,
						},
					},
					Status: netv1.IngressStatus{
						LoadBalancer: netv1.IngressLoadBalancerStatus{
							Ingress: []netv1.IngressLoadBalancerIngress{
								{
									IP: expectedIPs[0],
								},
								{
									IP: expectedIPs[1],
								},
							},
						},
					},
				}
				cl := fake.NewClientBuilder().WithScheme(scheme.Scheme).WithObjects(coreDNSService).WithObjects(ing).Build()
				return cl
			},
		},

		{
			name:          "CoreDNSService IS NOT LoadBalancer and wrong Ingress Annotation",
			expectedError: true,
			expectedIPs:   []string{"10.0.0.1", "10.0.0.2"},
			hasIngress:    true,
			config: &resolver.Config{
				K8gbNamespace:      "k8gb",
				CoreDNSServiceType: corev1.ServiceTypeClusterIP,
			},
			setupClient: func(expectedIPs []string) client.Client {
				coreDNSService := &corev1.Service{
					ObjectMeta: metav1.ObjectMeta{
						Name:      objectMeta.Name,
						Namespace: objectMeta.Namespace,
						Labels: map[string]string{
							assistant.CoreDNSServiceLabelName: assistant.CoreDNSServiceLabelValue,
						},
					},
					Spec: corev1.ServiceSpec{
						Type: corev1.ServiceTypeClusterIP,
					},
					Status: corev1.ServiceStatus{
						LoadBalancer: corev1.LoadBalancerStatus{
							Ingress: []corev1.LoadBalancerIngress{
								{
									IP: expectedIPs[0],
								},
								{
									IP: expectedIPs[1],
								},
							},
						},
					},
				}
				ing := &netv1.Ingress{
					ObjectMeta: metav1.ObjectMeta{
						Name:      "coredns-ingress",
						Namespace: "k8gb",
					},
					Status: netv1.IngressStatus{
						LoadBalancer: netv1.IngressLoadBalancerStatus{
							Ingress: []netv1.IngressLoadBalancerIngress{
								{
									IP: expectedIPs[0],
								},
								{
									IP: expectedIPs[1],
								},
							},
						},
					},
				}
				cl := fake.NewClientBuilder().WithScheme(scheme.Scheme).WithObjects(coreDNSService).WithObjects(ing).Build()
				return cl
			},
		},
	}
	for _, test := range tests {
		t.Run(test.name, func(t *testing.T) {
			cl := test.setupClient(test.expectedIPs)
			bootstrap, err := GetBootstrapWithClient(ctx, test.config, cl)
			if test.expectedError {
				assert.Nil(t, bootstrap)
				assert.Error(t, err)
				return
			}
			assert.Equal(t, test.expectedIPs, bootstrap.IPs)
			assert.Equal(t, test.hasIngress, bootstrap.HasIngress())
		})
	}
}
